package com.dongdongshop.controller;

import com.dongdongshop.data.Page;
import com.dongdongshop.data.Result;
import com.dongdongshop.em.ResultEnum;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.service.TbItemCatService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("itemCat")
public class TbItemCatController {

    @DubboReference
    private TbItemCatService tbItemCatService;


    @RequestMapping("toitemCat")
    public String toitemCat() {
        return "/admin/item_cat";
    }

    @RequestMapping("itemCatList")
    @ResponseBody
    public Result itemCatList(Long pid, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize) {
        Page page = tbItemCatService.selectByPid(pid, pageNum, pageSize);
        if (page != null) {
            return Result.response(ResultEnum.SUCCESS).setData(page);
        }
        return Result.response(ResultEnum.ERROR);
    }

    @RequestMapping("insert")
    @ResponseBody
    public Result insert(TbItemCat tbItemCat){
        boolean b = tbItemCatService.insertPid(tbItemCat);
        if (b){
            return Result.response(ResultEnum.SUCCESS);
        }
        return Result.response(ResultEnum.ERROR);
        }


    @RequestMapping("toupdate")
    @ResponseBody
    public Result toupdate(Long id) {
        TbItemCat tbItemCat = tbItemCatService.selectById(id);
        if (tbItemCat != null) {
            return Result.response(ResultEnum.SUCCESS).setData(tbItemCat);
        }
        return Result.response(ResultEnum.ERROR);
    }

    @RequestMapping("update")
    @ResponseBody
    public Result update(TbItemCat tbItemCat){
        boolean b = tbItemCatService.updatePid(tbItemCat);
        if (b){
            return Result.response(ResultEnum.SUCCESS);
        }
        return Result.response(ResultEnum.ERROR);
    }

    @RequestMapping("delete")
    @ResponseBody
    public Result delete(@RequestParam("ids") Long[] ids){
        boolean b = tbItemCatService.deleteByIds(ids);
        if (b){
            return Result.response(ResultEnum.SUCCESS);
        }
        return Result.response(ResultEnum.ERROR);

    }
}
